$(function () {
  //抽取js变量
  var details_datagrid = $("#details_datagrid");
  var details_dialog = $("#details_dialog");
  var details_form = $("#details_form");


  details_datagrid.datagrid({
    url: '/dictionaries/getDictionariesSnByDetails.do',
    fitColumns: true,
    fit: true,//自动填充
    toolbar: '#details_toolbar',
    pagination: true,
    rownumbers: true, //显示一个行号列。
    singleSelect: true,//只允许选择一行
    striped: true, //显示斑马线效果
    columns: [[
      {field: 'name', title: '明细名称', width: 100},
      {field: 'info', title: '明细简介', width: 100},
      {
        field: 'directory', title: '所属目录', width: 100, formatter: function (value) {
          return value ? value.name : "";
        }
      },
    ]],

  });
  var methodObj = {
    add_item: function () {
      //打开弹出框
      details_dialog.dialog('open');
      var row = $("#dictionaries_datagrid").datagrid('getSelected');
      //设置标题
      details_form.form('load', {'directory.id':row.id});
      details_dialog.dialog('setTitle', '新增菜单');
    },

    reload_item:function () {
      details_datagrid.datagrid('load');
    },
    save_item: function () {
      //提交表单
      details_form.form('submit', {
        url: '/details/saveOrUpdate.do',
        onSubmit: function (param) {
          //手动调用验证表单的方法
          return details_form.form('validate');
        },
        success: function (data) {
          data = $.parseJSON(data);
          if (data.success) {
            //提示用户操作结果
            $.messager.alert('温馨提示', '保存成功!', 'info', function () {
              //关闭弹出框
              methodObj.cancel_item();
              //让datagrid重新重新菜单数据
              details_datagrid.datagrid('reload');
              //同时刷新明细菜单数据
              $("#detailsItem_datagrid").datagrid('reload');
            })

          } else {
            $.messager.alert('温馨提示', data.msg, 'error')
          }
        },
      })
    },
    cancel_item: function () {
      details_dialog.dialog('close');
    }
  }


  $("a[data-item]").click(function () {
    var methodName = $(this).data("item");
    methodObj[methodName]();
  })


  details_dialog.dialog({
    width: 300,
    height: 385,
    buttons: '#form_btns',
    closed: true,
    onClose: function () {
      //清空表单
      details_form.form('clear');
    }
  })
})










